sessionStorage.ChannelCount = sessionStorage.ChannelListIds?sessionStorage.ChannelListIds.split(',').length:0;
$('.btn-success,.btn-danger').click(function(){
	var classth = $(this).attr('class');
	var id = $(this).attr('channelid');
	if(classth.split(" ")[1]=='btn-success'){
		action(id,1,['通过审核','通过审核后即为激活状态。']);
	}else if(classth.split(" ")[1]=='btn-danger'){
		action(id,2,['拒绝通过','拒绝后将不再显示该数据！']);
	}else{
		sweetAlert('','不能识别的操作！','error');
	}
});

function action(id,status,data) {
	swal({ 
	  title: "你确认执行"+data[0]+"操作吗？", 
	  text: data[1], 
	  type: "warning",
	  showCancelButton: true, 
	  confirmButtonColor: "#DD6B55",
	  confirmButtonText: "是的", 
	  cancelButtonText: "取消",
	  closeOnConfirm: false
	},
	function(isConfirm){ 
	  if (isConfirm) { //通过
	  	swal({ 
          title: "备注", 
          text: "请填写"+data[0]+"的理由",
          type: "input", 
          confirmButtonText: "好了",
          cancelButtonText: "不填",
          showCancelButton: true, 
          closeOnConfirm: false, 
          animation: "slide-from-top", 
          inputPlaceholder: "请简单说明为什么"+data[0]+"。"
        },
        function(inputValue){
          	data_post = {'wayid':id,'status':status,'remark':inputValue};
          	action_send(data_post);
        });
	  }
	});
	
}

function action_send(data_post) {
	ajax({
		'url': '/Admin/Channel/ShenheFK',
		'type':'post',
		'data':data_post,
		success: function (response) {
		    if(response.success=='OK'){
		    	if(response.status=='1'){
		    		sweetAlert('',response.message,'success');
		    		setTimeout(function () {
		              location.href = '/admin/channel/channellistindex';
		            },5000);
		    	}else if(response.status=='2'){
		    		sweetAlert('',response.message,'success');
		    		setTimeout(function () {
		              location.href = '/admin/channel/channellistindex';
		            },5000);
		    	}
		      sweetAlert('',response.message,'success');
		    }else if(response.error){
		      sweetAlert('',response.message,'error');
		    }else{
		      sweetAlert('','系统故障，请稍后重试！','error');
		    }
		},
		'dataType': 'json'
	},true);
}

setInterval(function(){
	get('/Admin/Channel/ChannelCountAll',{},function(response){
		if(sessionStorage.ChannelCount<response){
			getlist();
			sessionStorage.ChannelCount = response;
		}
	});
},'10000');

function getlist() {
	var page = GetQueryString('p');
	var data;
	if(!page){
		data = {
			'p':page,
			'getall':'aj'
		};
	}else{
		data = {
			'getall':'aj'
		};
	}
	post('/Admin/Channel/ChannelListIndex',data,function(response){
		var channelList;
	    $('.tables_channel tr').each(function(){
	       channelList = channelList+','+$(this).attr('channelid');
	    });
	    var arrlist;
	    if(channelList){
	        arrlist = channelList.split(',');
	        arrlist.shift();
	    }
    	for (var i = 0; i < response.length; i++) {
    		if(arrlist){
    			if($.inArray(response[i].id,arrlist)<0){
	    			var newtr = '<tr channelid="'+response[i].d['id']+'"><td>'+response[i].d['waynum']+'</td><td>'+response[i].d['name']+'</td><td>'+response[i].d['create_time']+'</td><td>'+response[i].d['admin_name']+'</td><td>'+response[i].d['admin_id']+'</td><td>'+response[i].d['status']+'</td><td><a href="/admin/channel/ChannelDetailskf?id='+response[i].d['id']+'">审核</a></td></tr>';
	    			$('.tables_channel').append(newtr);
	    		}
    		}else{
    			var newtr = '<tr channelid="'+response[i].d['id']+'"><td>'+response[i].d['waynum']+'</td><td>'+response[i].d['name']+'</td><td>'+response[i].d['create_time']+'</td><td>'+response[i].d['admin_name']+'</td><td>'+response[i].d['admin_id']+'</td><td>'+response[i].d['status']+'</td><td><a href="/admin/channel/ChannelDetailskf?id='+response[i].d['id']+'">审核</a></td></tr>';
	    		$('.tables_channel').append(newtr);
    		}
    	}
	},'json');
}